﻿Imports System.IO
Imports System.ComponentModel

Public Class frmDirectorySelect

    Private allowClose As Boolean = False

    Private Sub frmDirectorySelect_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        If Not allowClose AndAlso tvwTree.SelectedNode Is Nothing Then
            MessageBox.Show("Select folder please.")
            e.Cancel = True
        ElseIf Not allowClose Then
            Me.DialogResult = DialogResult.OK
        End If

    End Sub

    Private Sub frmDirectorySelect_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.LostFocus
        tvwTree.Nodes(0).Expand()
    End Sub

    Private Sub trDirectory_AfterCollapse(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tvwTree.AfterCollapse
        e.Node.ImageIndex = 0
        e.Node.SelectedImageIndex = 0
    End Sub

    Private Sub trDirectory_AfterExpand(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tvwTree.AfterExpand
        e.Node.ImageIndex = 1
        e.Node.SelectedImageIndex = 1
    End Sub

    Private Sub trDirectory_BeforeExpand(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles tvwTree.BeforeExpand
        If e.Node.Nodes.Count = 1 AndAlso e.Node.Nodes(0).ImageIndex < 0 Then
            e.Node.Nodes.Clear()
            Dim directories As String()
            Try
                directories = Directory.GetDirectories(e.Node.FullPath.Replace("\\", "\"))
            Catch
                e.Cancel = True
                Return
            End Try
            For Each folder As String In directories
                Dim node = e.Node.Nodes.Add(Path.GetFileName(folder))
                node.SelectedImageIndex = 0
                node.ImageIndex = 0
                Dim subnode = node.Nodes.Add("Please Wait")
                subnode.ImageIndex = -1
                subnode.SelectedImageIndex = -2
                subnode.ForeColor = SystemColors.GrayText
            Next
        End If

    End Sub

    Public Property SelectedPath() As String
        Get
            Return If(tvwTree.SelectedNode Is Nothing, Nothing, tvwTree.SelectedNode.FullPath.Replace("\\", "\"))
        End Get
        Set(ByVal value As String)
            If value Is Nothing Then
                Throw New ArgumentNullException("value")
            End If
            If Not Path.IsPathRooted(value) Then
                Throw New ArgumentException("Path must be rooted")
            End If
            Dim newPath As String = value.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar)
            Dim segments As String() = newPath.Split(Path.DirectorySeparatorChar)
            Dim node As TreeNode = tvwTree.Nodes(0)
            Dim I As Integer = 0
            For Each segment As String In segments
                If I = 0 AndAlso segment = "" Then
                    Continue For
                End If
                node.Expand()
                Dim found As Boolean = False
                For Each subnode As TreeNode In node.Nodes
                    If subnode.Text.ToLower() = segment.ToLower() Then
                        node = subnode
                        found = True
                        'inner for
                        Exit For
                    End If
                Next
                If Not found Then
                    Exit For
                End If
                I += 1
            Next
            tvwTree.SelectedNode = node
            node.EnsureVisible()
        End Set
    End Property

    

    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
        If tvwTree.SelectedNode Is Nothing Then
            MessageBox.Show("Select folder please ...")
            Return
        End If
        allowClose = True
        Me.Close()
        Me.DialogResult = DialogResult.OK
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        allowClose = True
        Me.Close()
        Me.DialogResult = DialogResult.Cancel
    End Sub
End Class